package LeetCode.month01.day30;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class No1695 {
    public static void main(String[] args) {
        System.out.println(maximumUniqueSubarray(new int[]{5,2,1,2,5,2,1,2,5}));
    }
    public static int maximumUniqueSubarray(int[] nums) {
        int n = nums.length;
        Set<Integer> set = new HashSet<>();
        int res = 0;
        int left = 0;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
            if(set.contains(nums[i])){
                while(nums[left]!=nums[i]) {
                    sum-=nums[left];
                    set.remove(nums[left]);
                    left++;
                }
                sum-=nums[left];
                left++;
            }else{
                set.add(nums[i]);
            }
            res= Math.max(res,sum);
        }
        return res;
    }
}
